<template>
  <view>
    <!-- click原生事件 只能给原生标签注册 不能给组件注册 => el-button @click -->
    <!-- 
      组件可以@click: 
      1. @click.native
      2. 组件内部触发一个自定义事件 click
     -->
    <div class="box">
      <MySearch @click="goSearch"></MySearch>
    </div>
    
    <!-- 轮播图区域 -->
    <swiper
      class="top-swiper"
      :indicator-dots="true"
      :autoplay="true"
      :interval="3000"
      :duration="1000"
      :circular="true"
    >
      <!-- 循环渲染轮播图的 item 项 -->
      <swiper-item v-for="item in swiperList" :key="item.goods_id">
        <navigator
          :url="`/pages_sub1/product-detail/product-detail?pid=${item.goods_id}`"
          class="swiper-content"
        >
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>

    <!-- 分类导航区域 -->
    <view class="nav-list">
      <view
        class="nav-item"
        v-for="(item, index) in navList"
        :key="index"
        @click="go(item)"
      >
        <image :src="item.image_src" class="nav-img"></image>
      </view>
    </view>

    <!-- 楼层区域 -->
    <view class="floor-list">
      <!-- 楼层 item 项 -->
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <!-- 楼层标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>

        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <!-- 左侧 1 个大图 -->
          <view class="left-img-box">
            <image
              :src="item.product_list[0].image_src"
              :style="{ width: item.product_list[0].image_width + 'rpx' }"
              mode="widthFix"
              @click="goToProductList(item.product_list[0])"
            ></image>
          </view>

          <!-- 右侧 4 个小图 -->
          <view class="right-img-box">
            <!-- v-for  v-if 不能连用 => 性能极大地消耗 -->
            <block v-for="(product, idx) in item.product_list" :key="idx" >
              <view class="right-img-item" v-if="idx !== 0">
                <image
                  :src="product.image_src"
                  :style="{ width: product.image_width + 'rpx' }"
                  mode="widthFix"
                  @click="goToProductList(product)"
                ></image>
              </view>
            </block>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import MySearch from '../../components/my-search.vue'
import { mapGetters } from 'vuex'
export default {
  components: {
    MySearch
  },

  computed: {
    ...mapGetters('cart', ['total']),
  },

  onShow() {
    uni.setTabBarBadge({
      index: 2,
      text: '' + this.$store.getters['cart/total'],
    })
  },

  watch: {
    total: {
      handler(newVal) {
        if (newVal > 0) {
          // 大于0的时候才显示徽标
          uni.setTabBarBadge({
            index: 2, // tabBar的按钮索引，也就是第三个按钮
            text: "" + newVal, // 必须是字符串值！！！
          });
        } else {
          // 小于等于0则删除徽标
          uni.removeTabBarBadge({
            index: 2
          })
        }
      },
      immediate: true
    }
  },
  
  data() {
    return {
      swiperList: [],
      // 1. 分类导航的数据列表
      navList: [],
      // 1. 楼层的数据列表
      floorList: [],
    }
  },

  created() {
    this.getSwiperList()
    // 2. 在页面刚加载的时候，调用获取分类导航数据的方法
    this.getNavList()
    // 2. 在页面刚加载的时候，调用获取楼层数据的方法
    this.getFloorList()
  },

  methods: {
    goSearch() {
      uni.navigateTo({
        url: '/pages_sub1/search/search'
      })
    },
    goToProductList(product) {
      // console.log(product)
      const str = product.navigator_url.split('?')[1]
      // console.log(str)
      uni.navigateTo({
        url: '/pages_sub1/product-list/product-list?'+ str
      })
    },
    
    // 3. 获取楼层数据的方法
    async getFloorList() {
      const { meta, message } = await this.$http.get('/home/floordata')

      // 请求失败处理
      if (meta.status !== 200) {
        return this.$msg()
      }

      this.floorList = message
    },
    go(item) {
      // console.log(item)
      if (item.name === '分类') {
        uni.switchTab({
          url: '/pages/category/category',
        })
      }
    },

    async getNavList() {
      const { meta, message } = await this.$http.get('/home/catitems')

      // 请求失败处理
      if (meta.status !== 200) {
        return this.$msg()
      }

      this.navList = message
    },
    async getSwiperList() {
      const res = await this.$http.get('/home/swiperdata')
      console.log(res) // res.message  res.meta.status

      if (res.meta.status !== 200) return this.$msg()

      this.swiperList = res.message
    },
  },
}
</script>

<style lang="scss">

.box {
  position: sticky;

  top: 0;

  z-index: 999;
}

.top-swiper {
  height: 330rpx;

  .swiper-content,
  image {
    width: 100%;
    height: 100%;
  }
}

.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 30rpx 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-title {
  display: flex;
  width: 100%;
  height: 60rpx;
}


.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
</style>
